{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(model=\"qwen-max-latest\", temperature=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "from IPython.display import display, Markdown"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基本思维链提示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Standard prompt\n",
    "standard_prompt = PromptTemplate(\n",
    "    input_variables=[\"question\"],\n",
    "    template=\"简洁地回答以下问题：{question}。\",\n",
    ")\n",
    "\n",
    "# Chain of Thought prompt\n",
    "cot_prompt = PromptTemplate(\n",
    "    input_variables=[\"question\"],\n",
    "    template=\"简明扼要地逐步回答以下问题：{question}。\",\n",
    ")\n",
    "\n",
    "# Create chains\n",
    "standard_chain = standard_prompt | llm\n",
    "cot_chain = cot_prompt | llm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "标准回复:\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "平均速度 = 总路程 ÷ 总时间  \n",
       "总路程 = 120 公里，总时间 = 2 小时  \n",
       "\n",
       "平均速度 = 120 ÷ 2 = **60 公里/小时**\n",
       "\n",
       "**最终答案：**  \n",
       "$\\boxed{60}$"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "==================================================\n",
      "\n",
      "\n",
      "思维链回复:\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "我们可以通过以下步骤计算火车的平均速度：\n",
       "\n",
       "### 第一步：明确公式\n",
       "平均速度的公式是：\n",
       "$$\n",
       "\\text{平均速度} = \\frac{\\text{总路程}}{\\text{总时间}}\n",
       "$$\n",
       "\n",
       "### 第二步：代入已知数据\n",
       "题目中给出的总路程是 120 公里，总时间是 2 小时。将这些值代入公式：\n",
       "$$\n",
       "\\text{平均速度} = \\frac{120 \\, \\text{公里}}{2 \\, \\text{小时}}\n",
       "$$\n",
       "\n",
       "### 第三步：进行计算\n",
       "$$\n",
       "\\text{平均速度} = 60 \\, \\text{公里/小时}\n",
       "$$\n",
       "\n",
       "### 最终答案：\n",
       "$$\n",
       "\\boxed{60 \\, \\text{公里/小时}}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Example question\n",
    "question = \"如果一列火车在 2 小时内行驶 120 公里，那么它的平均速度是多少公里/小时？\"\n",
    "\n",
    "# Get responses\n",
    "standard_response = standard_chain.invoke(question).content\n",
    "cot_response = cot_chain.invoke(question).content\n",
    "\n",
    "print(\"标准回复:\")\n",
    "display(Markdown(standard_response))\n",
    "print(\"\\n\" + \"=\" * 50 + \"\\n\")\n",
    "print(\"\\n思维链回复:\")\n",
    "display(Markdown(cot_response))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 高级思维链技术"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "### **步骤 1：说明计算内容**\n",
       "我们需要计算整个行程的平均速度。平均速度的定义是总路程除以总时间。\n",
       "\n",
       "公式为：\n",
       "$$\n",
       "\\text{平均速度} = \\frac{\\text{总路程}}{\\text{总时间}}\n",
       "$$\n",
       "\n",
       "### **步骤 2：计算总路程**\n",
       "汽车行驶了两段路程：\n",
       "- 第一段路程为 $150$ 公里。\n",
       "- 第二段路程为 $100$ 公里。\n",
       "\n",
       "总路程为：\n",
       "$$\n",
       "\\text{总路程} = 150 + 100 = 250 \\, \\text{公里}\n",
       "$$\n",
       "\n",
       "**结果解释：**\n",
       "汽车在整个行程中总共行驶了 $250$ 公里。\n",
       "\n",
       "---\n",
       "\n",
       "### **步骤 3：计算每段路程所需的时间**\n",
       "根据公式：\n",
       "$$\n",
       "\\text{时间} = \\frac{\\text{路程}}{\\text{速度}}\n",
       "$$\n",
       "\n",
       "#### **第一段路程的时间**\n",
       "第一段路程的速度为 $60 \\, \\text{公里/小时}$，路程为 $150 \\, \\text{公里}$，因此时间为：\n",
       "$$\n",
       "t_1 = \\frac{150}{60} = 2.5 \\, \\text{小时}\n",
       "$$\n",
       "\n",
       "#### **第二段路程的时间**\n",
       "第二段路程的速度为 $50 \\, \\text{公里/小时}$，路程为 $100 \\, \\text{公里}$，因此时间为：\n",
       "$$\n",
       "t_2 = \\frac{100}{50} = 2 \\, \\text{小时}\n",
       "$$\n",
       "\n",
       "**结果解释：**\n",
       "汽车在第一段路程中用了 $2.5$ 小时，在第二段路程中用了 $2$ 小时。\n",
       "\n",
       "---\n",
       "\n",
       "### **步骤 4：计算总时间**\n",
       "总时间为两段路程所需时间之和：\n",
       "$$\n",
       "\\text{总时间} = t_1 + t_2 = 2.5 + 2 = 4.5 \\, \\text{小时}\n",
       "$$\n",
       "\n",
       "**结果解释：**\n",
       "汽车在整个行程中总共用了 $4.5$ 小时。\n",
       "\n",
       "---\n",
       "\n",
       "### **步骤 5：计算平均速度**\n",
       "根据公式：\n",
       "$$\n",
       "\\text{平均速度} = \\frac{\\text{总路程}}{\\text{总时间}}\n",
       "$$\n",
       "\n",
       "将已知值代入公式：\n",
       "$$\n",
       "\\text{平均速度} = \\frac{250}{4.5} \\approx 55.56 \\, \\text{公里/小时}\n",
       "$$\n",
       "\n",
       "**结果解释：**\n",
       "汽车在整个行程中的平均速度约为 $55.56 \\, \\text{公里/小时}$。\n",
       "\n",
       "---\n",
       "\n",
       "### **最终答案**\n",
       "$$\n",
       "\\boxed{55.56 \\, \\text{公里/小时}}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "advanced_cot_prompt = PromptTemplate(\n",
    "    input_variables=[\"question\"],\n",
    "    template=\"\"\"逐步解决以下问题。对于每个步骤：\n",
    "1. 说明你要计算的内容\n",
    "2. 写下你将使用的公式（如果适用）\n",
    "3. 执行计算\n",
    "4. 解释结果\n",
    "\n",
    "问题：{question}\n",
    "\n",
    "解决方案：\"\"\",\n",
    ")\n",
    "\n",
    "advanced_cot_chain = advanced_cot_prompt | llm\n",
    "\n",
    "complex_question = \"一辆汽车以 60 公里/小时的速度行驶 150 公里，然后以 50 公里/小时的速度行驶 100 公里。整个行程的平均速度是多少？\"\n",
    "\n",
    "advanced_cot_response = advanced_cot_chain.invoke(complex_question).content\n",
    "display(Markdown(advanced_cot_response))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 比较分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "标准回复:\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "### 解答过程：\n",
       "\n",
       "1. **计算水箱的总体积**  \n",
       "   圆柱体积公式为：  \n",
       "   $$\n",
       "   V = \\pi r^2 h\n",
       "   $$  \n",
       "   已知半径 $r = 1.5$ 米，高 $h = 4$ 米，$\\pi = 3.14159$。代入公式：  \n",
       "   $$\n",
       "   V = 3.14159 \\times (1.5)^2 \\times 4 = 3.14159 \\times 2.25 \\times 4 = 28.27431 \\, \\text{立方米}\n",
       "   $$\n",
       "\n",
       "2. **计算当前水量**  \n",
       "   水箱已装满 $ \\frac{2}{3} $，因此当前水量为：  \n",
       "   $$\n",
       "   V_{\\text{当前}} = \\frac{2}{3} \\times 28.27431 = 18.84954 \\, \\text{立方米}\n",
       "   $$\n",
       "\n",
       "3. **计算剩余可加水量**  \n",
       "   剩余空间的体积为：  \n",
       "   $$\n",
       "   V_{\\text{剩余}} = 28.27431 - 18.84954 = 9.42477 \\, \\text{立方米}\n",
       "   $$\n",
       "\n",
       "4. **将剩余体积换算为升**  \n",
       "   1 立方米 = 1000 升，因此：  \n",
       "   $$\n",
       "   V_{\\text{剩余}} = 9.42477 \\times 1000 = 9424.77 \\, \\text{升}\n",
       "   $$\n",
       "\n",
       "5. **计算加水时间**  \n",
       "   加水速度为每分钟 10 升，所需时间为：  \n",
       "   $$\n",
       "   t = \\frac{9424.77}{10} = 942.477 \\, \\text{分钟}\n",
       "   $$\n",
       "\n",
       "6. **将时间转换为小时和分钟**  \n",
       "   将分钟转换为小时和分钟：  \n",
       "   $$\n",
       "   942.477 \\, \\text{分钟} = 15 \\, \\text{小时} + 42.477 \\, \\text{分钟}\n",
       "   $$  \n",
       "   四舍五入到最接近的分钟，得到：  \n",
       "   $$\n",
       "   15 \\, \\text{小时} \\, 42 \\, \\text{分钟}\n",
       "   $$\n",
       "\n",
       "### 最终答案：\n",
       "$$\n",
       "\\boxed{15 \\, \\text{小时} \\, 42 \\, \\text{分钟}}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "==================================================\n",
      "\n",
      "\n",
      "思维链回复:\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "### 步骤 1: 计算圆柱形水箱的总体积\n",
       "#### 说明：\n",
       "我们需要计算圆柱形水箱的总体积，公式为：\n",
       "$$\n",
       "V_{\\text{total}} = \\pi r^2 h\n",
       "$$\n",
       "其中 $r$ 是半径，$h$ 是高度。\n",
       "\n",
       "#### 公式：\n",
       "$$\n",
       "V_{\\text{total}} = \\pi r^2 h\n",
       "$$\n",
       "\n",
       "#### 计算：\n",
       "已知 $r = 1.5 \\, \\text{m}$，$h = 4 \\, \\text{m}$，$\\pi = 3.14159$。代入公式：\n",
       "$$\n",
       "V_{\\text{total}} = 3.14159 \\times (1.5)^2 \\times 4\n",
       "$$\n",
       "$$\n",
       "V_{\\text{total}} = 3.14159 \\times 2.25 \\times 4\n",
       "$$\n",
       "$$\n",
       "V_{\\text{total}} = 3.14159 \\times 9 = 28.27431 \\, \\text{m}^3\n",
       "$$\n",
       "\n",
       "#### 解释结果：\n",
       "水箱的总体积为 $28.27431 \\, \\text{m}^3$。\n",
       "\n",
       "---\n",
       "\n",
       "### 步骤 2: 计算水箱中已有水的体积\n",
       "#### 说明：\n",
       "水箱已经装满 $2/3$ 的水，因此我们计算水箱总体积的 $2/3$。\n",
       "\n",
       "#### 公式：\n",
       "$$\n",
       "V_{\\text{current}} = \\frac{2}{3} V_{\\text{total}}\n",
       "$$\n",
       "\n",
       "#### 计算：\n",
       "$$\n",
       "V_{\\text{current}} = \\frac{2}{3} \\times 28.27431\n",
       "$$\n",
       "$$\n",
       "V_{\\text{current}} = 18.84954 \\, \\text{m}^3\n",
       "$$\n",
       "\n",
       "#### 解释结果：\n",
       "水箱中当前已有水的体积为 $18.84954 \\, \\text{m}^3$。\n",
       "\n",
       "---\n",
       "\n",
       "### 步骤 3: 计算剩余可容纳的水体积\n",
       "#### 说明：\n",
       "水箱的剩余可容纳水体积是总体积减去当前已有水的体积。\n",
       "\n",
       "#### 公式：\n",
       "$$\n",
       "V_{\\text{remaining}} = V_{\\text{total}} - V_{\\text{current}}\n",
       "$$\n",
       "\n",
       "#### 计算：\n",
       "$$\n",
       "V_{\\text{remaining}} = 28.27431 - 18.84954\n",
       "$$\n",
       "$$\n",
       "V_{\\text{remaining}} = 9.42477 \\, \\text{m}^3\n",
       "$$\n",
       "\n",
       "#### 解释结果：\n",
       "水箱还能容纳 $9.42477 \\, \\text{m}^3$ 的水。\n",
       "\n",
       "---\n",
       "\n",
       "### 步骤 4: 将剩余体积转换为升\n",
       "#### 说明：\n",
       "因为加水速度以升为单位，我们需要将剩余体积从立方米转换为升。\n",
       "已知 $1 \\, \\text{m}^3 = 1000 \\, \\text{L}$。\n",
       "\n",
       "#### 公式：\n",
       "$$\n",
       "V_{\\text{remaining, L}} = V_{\\text{remaining}} \\times 1000\n",
       "$$\n",
       "\n",
       "#### 计算：\n",
       "$$\n",
       "V_{\\text{remaining, L}} = 9.42477 \\times 1000\n",
       "$$\n",
       "$$\n",
       "V_{\\text{remaining, L}} = 9424.77 \\, \\text{L}\n",
       "$$\n",
       "\n",
       "#### 解释结果：\n",
       "水箱还能容纳 $9424.77 \\, \\text{L}$ 的水。\n",
       "\n",
       "---\n",
       "\n",
       "### 步骤 5: 计算加满水所需的时间\n",
       "#### 说明：\n",
       "我们知道每分钟加水 $10 \\, \\text{L}$，因此需要计算加 $9424.77 \\, \\text{L}$ 水所需的时间。\n",
       "\n",
       "#### 公式：\n",
       "$$\n",
       "t_{\\text{minutes}} = \\frac{V_{\\text{remaining, L}}}{\\text{加水速度}}\n",
       "$$\n",
       "\n",
       "#### 计算：\n",
       "$$\n",
       "t_{\\text{minutes}} = \\frac{9424.77}{10} = 942.477 \\, \\text{分钟}\n",
       "$$\n",
       "\n",
       "#### 解释结果：\n",
       "加满水需要 $942.477 \\, \\text{分钟}$。\n",
       "\n",
       "---\n",
       "\n",
       "### 步骤 6: 将时间转换为小时和分钟\n",
       "#### 说明：\n",
       "将总时间从分钟转换为小时和分钟。已知 $1 \\, \\text{小时} = 60 \\, \\text{分钟}$。\n",
       "\n",
       "#### 公式：\n",
       "$$\n",
       "t_{\\text{hours}} = \\lfloor t_{\\text{minutes}} / 60 \\rfloor\n",
       "$$\n",
       "$$\n",
       "t_{\\text{minutes remaining}} = t_{\\text{minutes}} \\mod 60\n",
       "$$\n",
       "\n",
       "#### 计算：\n",
       "$$\n",
       "t_{\\text{hours}} = \\lfloor 942.477 / 60 \\rfloor = 15 \\, \\text{小时}\n",
       "$$\n",
       "$$\n",
       "t_{\\text{minutes remaining}} = 942.477 \\mod 60 = 42.477 \\, \\text{分钟}\n",
       "$$\n",
       "四舍五入后，$t_{\\text{minutes remaining}} = 42 \\, \\text{分钟}$。\n",
       "\n",
       "#### 解释结果：\n",
       "加满水需要 $15 \\, \\text{小时} \\, 42 \\, \\text{分钟}$。\n",
       "\n",
       "---\n",
       "\n",
       "### 最终答案：\n",
       "$$\n",
       "\\boxed{15 \\, \\text{小时} \\, 42 \\, \\text{分钟}}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "challenging_question = \"\"\"\n",
    "一个圆柱形水箱，半径为 1.5 米，高为 4 米，水箱已装满 2/3。\n",
    "如果以每分钟 10 升的速度加水，水箱需要多长时间才能溢出？\n",
    "以小时和分钟为单位给出答案，四舍五入到最接近的分钟。\n",
    "（π 使用 3.14159，1000 升 = 1 立方米）\"\"\"\n",
    "\n",
    "standard_response = standard_chain.invoke(challenging_question).content\n",
    "cot_response = advanced_cot_chain.invoke(challenging_question).content\n",
    "\n",
    "print(\"标准回复:\")\n",
    "display(Markdown(standard_response))\n",
    "print(\"\\n\" + \"=\" * 50 + \"\\n\")\n",
    "print(\"\\n思维链回复:\")\n",
    "display(Markdown(cot_response))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 问题解决应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "我们将按照您提供的步骤，系统地分析这个逻辑谜题并得出结论。\n",
       "\n",
       "---\n",
       "\n",
       "### **1. 列出事实：**\n",
       "\n",
       "#### 给定的信息和陈述：\n",
       "- 房间里有三个人：艾米、鲍勃和查理。\n",
       "- 其中一个人**总是说真话**（称为“真话者”）。\n",
       "- 另一个人**总是撒谎**（称为“撒谎者”）。\n",
       "- 还有一个人**有时说真话，有时撒谎**（称为“混合者”）。\n",
       "- 每个人的陈述如下：\n",
       "  - 艾米说：“鲍勃是个骗子。”\n",
       "  - 鲍勃说：“查理说真话和撒谎。”\n",
       "  - 查理说：“艾米和我都是骗子。”\n",
       "\n",
       "#### 所涉及的角色或元素：\n",
       "- 艾米、鲍勃、查理。\n",
       "\n",
       "#### 可能的角色或条件：\n",
       "- 真话者：总是说真话。\n",
       "- 撒谎者：总是撒谎。\n",
       "- 混合者：有时说真话，有时撒谎。\n",
       "\n",
       "#### 注意约束：\n",
       "- 每个人必须是真话者、撒谎者或混合者之一。\n",
       "- 每种角色只能分配给一个人。\n",
       "- 每个人的陈述必须与其角色一致。\n",
       "\n",
       "---\n",
       "\n",
       "### **2. 生成可能的场景：**\n",
       "\n",
       "我们需要考虑所有可能的角色分配组合。总共有 $3! = 6$ 种排列方式，分别是：\n",
       "\n",
       "1. 艾米 = 真话者，鲍勃 = 撒谎者，查理 = 混合者。\n",
       "2. 艾米 = 真话者，鲍勃 = 混合者，查理 = 撒谎者。\n",
       "3. 艾米 = 撒谎者，鲍勃 = 真话者，查理 = 混合者。\n",
       "4. 艾米 = 撒谎者，鲍勃 = 混合者，查理 = 真话者。\n",
       "5. 艾米 = 混合者，鲍勃 = 真话者，查理 = 撒谎者。\n",
       "6. 艾米 = 混合者，鲍勃 = 撒谎者，查理 = 真话者。\n",
       "\n",
       "---\n",
       "\n",
       "### **3. 测试每个场景：**\n",
       "\n",
       "#### 场景 1：艾米 = 真话者，鲍勃 = 撒谎者，查理 = 混合者\n",
       "- **艾米的陈述**：“鲍勃是个骗子。”  \n",
       "  - 如果艾米是真话者，则鲍勃确实是撒谎者。这与假设一致。\n",
       "- **鲍勃的陈述**：“查理说真话和撒谎。”  \n",
       "  - 如果鲍勃是撒谎者，则他的陈述必然是假的。这意味着查理**不是**混合者。这与假设矛盾。\n",
       "- **查理的陈述**：“艾米和我都是骗子。”  \n",
       "  - 如果查理是混合者，他可以撒谎。但根据他的陈述，“艾米是骗子”这部分是假的（因为艾米是真话者），而“我是骗子”可能是真的。这与假设部分一致。\n",
       "- **结论**：此场景不成立，因为鲍勃的陈述导致矛盾。\n",
       "\n",
       "---\n",
       "\n",
       "#### 场景 2：艾米 = 真话者，鲍勃 = 混合者，查理 = 撒谎者\n",
       "- **艾米的陈述**：“鲍勃是个骗子。”  \n",
       "  - 如果艾米是真话者，则鲍勃确实是个骗子。这与假设部分一致，因为混合者有时撒谎。\n",
       "- **鲍勃的陈述**：“查理说真话和撒谎。”  \n",
       "  - 如果鲍勃是混合者，他可能在撒谎。这意味着查理**不是**混合者，而是撒谎者。这与假设一致。\n",
       "- **查理的陈述**：“艾米和我都是骗子。”  \n",
       "  - 如果查理是撒谎者，则他的陈述必然是假的。这意味着“艾米是骗子”和“我是骗子”这两部分都必须为假。但实际上，艾米是真话者，而查理确实是撒谎者。因此，这与假设矛盾。\n",
       "- **结论**：此场景不成立，因为查理的陈述导致矛盾。\n",
       "\n",
       "---\n",
       "\n",
       "#### 场景 3：艾米 = 撒谎者，鲍勃 = 真话者，查理 = 混合者\n",
       "- **艾米的陈述**：“鲍勃是个骗子。”  \n",
       "  - 如果艾米是撒谎者，则她的陈述必然是假的。这意味着鲍勃**不是**撒谎者，而是真话者。这与假设一致。\n",
       "- **鲍勃的陈述**：“查理说真话和撒谎。”  \n",
       "  - 如果鲍勃是真话者，则他的陈述必然是真的。这意味着查理确实是混合者。这与假设一致。\n",
       "- **查理的陈述**：“艾米和我都是骗子。”  \n",
       "  - 如果查理是混合者，他可以说真话或撒谎。在他的陈述中，“艾米是骗子”是真的（因为艾米是撒谎者），而“我是骗子”可能是假的（因为他有时说真话）。这与假设一致。\n",
       "- **结论**：此场景成立。\n",
       "\n",
       "---\n",
       "\n",
       "#### 场景 4：艾米 = 撒谎者，鲍勃 = 混合者，查理 = 真话者\n",
       "- **艾米的陈述**：“鲍勃是个骗子。”  \n",
       "  - 如果艾米是撒谎者，则她的陈述必然是假的。这意味着鲍勃**不是**撒谎者，而是混合者。这与假设一致。\n",
       "- **鲍勃的陈述**：“查理说真话和撒谎。”  \n",
       "  - 如果鲍勃是混合者，他可能在撒谎。这意味着查理**不是**混合者，而是真话者。这与假设一致。\n",
       "- **查理的陈述**：“艾米和我都是骗子。”  \n",
       "  - 如果查理是真话者，则他的陈述必然是真的。这意味着“艾米是骗子”和“我是骗子”这两部分都必须为真。但实际上，查理是真话者，而不是骗子。因此，这与假设矛盾。\n",
       "- **结论**：此场景不成立，因为查理的陈述导致矛盾。\n",
       "\n",
       "---\n",
       "\n",
       "#### 场景 5：艾米 = 混合者，鲍勃 = 真话者，查理 = 撒谎者\n",
       "- **艾米的陈述**：“鲍勃是个骗子。”  \n",
       "  - 如果艾米是混合者，她可能在撒谎。这意味着鲍勃**不是**撒谎者，而是真话者。这与假设一致。\n",
       "- **鲍勃的陈述**：“查理说真话和撒谎。”  \n",
       "  - 如果鲍勃是真话者，则他的陈述必然是真的。这意味着查理确实是撒谎者。这与假设一致。\n",
       "- **查理的陈述**：“艾米和我都是骗子。”  \n",
       "  - 如果查理是撒谎者，则他的陈述必然是假的。这意味着“艾米是骗子”和“我是骗子”这两部分都必须为假。但实际上，艾米是混合者，而查理确实是撒谎者。因此，这与假设矛盾。\n",
       "- **结论**：此场景不成立，因为查理的陈述导致矛盾。\n",
       "\n",
       "---\n",
       "\n",
       "#### 场景 6：艾米 = 混合者，鲍勃 = 撒谎者，查理 = 真话者\n",
       "- **艾米的陈述**：“鲍勃是个骗子。”  \n",
       "  - 如果艾米是混合者，她可能在说真话。这意味着鲍勃确实是撒谎者。这与假设一致。\n",
       "- **鲍勃的陈述**：“查理说真话和撒谎。”  \n",
       "  - 如果鲍勃是撒谎者，则他的陈述必然是假的。这意味着查理**不是**混合者，而是真话者。这与假设一致。\n",
       "- **查理的陈述**：“艾米和我都是骗子。”  \n",
       "  - 如果查理是真话者，则他的陈述必然是真的。这意味着“艾米是骗子”和“我是骗子”这两部分都必须为真。但实际上，查理是真话者，而不是骗子。因此，这与假设矛盾。\n",
       "- **结论**：此场景不成立，因为查理的陈述导致矛盾。\n",
       "\n",
       "---\n",
       "\n",
       "### **4. 消除不一致的场景：**\n",
       "\n",
       "通过测试所有场景，我们发现只有**场景 3**（艾米 = 撒谎者，鲍勃 = 真话者，查理 = 混合者）没有矛盾。\n",
       "\n",
       "---\n",
       "\n",
       "### **5. 总结解决方案：**\n",
       "\n",
       "- 艾米 = 撒谎者。\n",
       "- 鲍勃 = 真话者。\n",
       "- 查理 = 混合者。\n",
       "\n",
       "---\n",
       "\n",
       "### **6. 提供明确的答案：**\n",
       "\n",
       "$$\n",
       "\\boxed{\\text{艾米是撒谎者，鲍勃是真话者，查理是混合者。}}\n",
       "$$\n",
       "\n",
       "这是唯一可能的解决方案，因为其他所有场景都导致了矛盾或违反约束。"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "logical_reasoning_prompt = PromptTemplate(\n",
    "    input_variables=[\"scenario\"],\n",
    "    template=\"\"\"彻底分析以下逻辑谜题。在分析中遵循以下步骤：\n",
    "\n",
    "列出事实：\n",
    "\n",
    "清楚地总结所有给定的信息和陈述。\n",
    "识别所涉及的所有角色或元素。\n",
    "识别可能的角色或条件：\n",
    "\n",
    "确定适用于角色或元素的所有可能的角色、行为或状态（例如，说真话的人、撒谎者、两种都有）。\n",
    "注意约束：\n",
    "\n",
    "概述谜题中指定的任何规则、约束或关系。\n",
    "生成可能的场景：\n",
    "\n",
    "系统地考虑角色或元素的所有可能的角色或条件组合。\n",
    "确保所有排列都得到考虑。\n",
    "测试每个场景：\n",
    "\n",
    "对于每个可能的场景：\n",
    "假设你分配的角色或条件。\n",
    "根据这些假设分析每个语句。\n",
    "检查场景中的一致性或矛盾性。\n",
    "消除不一致的场景：\n",
    "\n",
    "丢弃任何导致矛盾或违反约束的场景。\n",
    "跟踪排除每个场景的理由。\n",
    "总结解决方案：\n",
    "\n",
    "确定测试后保持一致的场景。\n",
    "总结调查结果。\n",
    "提供明确的答案：\n",
    "\n",
    "明确说明每个角色或元素的作用或条件。\n",
    "根据您的分析，解释为什么这是唯一可能的解决方案。\n",
    "场景：\n",
    "\n",
    "{scenario}\n",
    "\n",
    "分析：\"\"\",\n",
    ")\n",
    "\n",
    "logical_reasoning_chain = logical_reasoning_prompt | llm\n",
    "\n",
    "logical_puzzle = \"\"\"房间里有三个人：艾米、鲍勃和查理。\n",
    "其中一个人总是说真话，另一个人总是撒谎，还有一个人说真话和撒谎。\n",
    "艾米说：“鲍勃是个骗子。”\n",
    "鲍勃说：“查理说真话和撒谎。”\n",
    "查理说：“艾米和我都是骗子。”\n",
    "确定每个人的本性（说真话的人、撒谎的人或说假话的人）。\"\"\"\n",
    "\n",
    "logical_reasoning_response = logical_reasoning_chain.invoke(logical_puzzle).content\n",
    "display(Markdown(logical_reasoning_response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "prompt",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
